<template>
 <!-- 二级类目的面包屑 -->
 <!-- 筛选模块 -->
 <ShaiXuan/>
 <!-- 排序模块 -->
  <Paixu class="paixu" @paixu="sortChange" />
  <ul>
    <li v-for="goods in goodsList" :key="goods.id" >
      <Fenlei :goods="goods" @paixu="sortChange" />
    </li>
  </ul>
  <!-- 无线加载组件模块 -->
  <XTXjiazhai :loading="loading" :finished="finished" @infinite="getData"/>
</template>
<script>
import Paixu from './paixu-sort.vue'
import ShaiXuan from './shaixuan-filter.vue'
import Fenlei from './fenlei-item.vue'
import { fenleishaixuanqinqiu } from '@/Api/index'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { computed, ref, watch } from 'vue'
export default {
  name: 'SubBread',
  components: { ShaiXuan, Paixu, Fenlei },
  setup () {
    // 在地址的路由中,获取二级分类的ID，
    const route = useRoute()
    // 获取vuex中的类目数据
    const store = useStore()
    //  通过计算属性得到，二级类目的名称和ID，一级类目的名称和ID
    const leimu = computed(() => {
      const obj = {}
      // 获取 vuex 的里面的gouwuche 值
      store.state.gouwuche.gouwuche.forEach(top => {
        top.children && top.children.forEach(sub => {
          if (sub.id === route.params.id) {
            // 设置二级类目
            obj.sub = { id: sub.id, name: sub.name }
            // 设置一级类目
            obj.top = { id: top.id, name: top.name }
          }
        })
      })
      return obj
    })
    //  任何筛选条件变化需要更新列表
    const loading = ref(false) // 加载中
    // 是否加载完毕
    const finished = ref(false)
    // 商品列表数据
    const goodsList = ref([])
    //  请求参数 查询参数
    let reqParams = {
      page: 1, //
      pageSize: 20
    }
    // 获取数据函数
    const getData = () => {
      loading.value = true // 开启正在加载中
      reqParams.categoryId = route.params.id
      fenleishaixuanqinqiu(reqParams).then(({ result }) => {
        if (result.items.length) {
          goodsList.value.push(...result.items)
          reqParams.page++
        } else {
          // 加载完毕
          finished.value = true
        }
        // 请求结束
        loading.value = false
      })
    }
    // 切换二级分类重新加载
    watch(() => route.params.id, (newVal) => {
      // 判断是否是二级类目
      getData()
      if (newVal && route.path === `/FenLei/ErjiZhanshi/${newVal}`) {
        goodsList.value = [] // 清空数据
        reqParams = {
          page: 1,
          pageSize: 20
        }
        finished.value = false // 是否加载完毕
      }
    })
    //
    // 1. 更改排序组件的筛选数据，重新请求
    const sortChange = (sortParams) => {
      finished.value = false
      // 合并请求参数，保留之前参数
      reqParams = { ...reqParams, ...sortParams }
      reqParams.page = 1
      goodsList.value = []
    }
    // 2. 更改筛选组件的筛选数据，重新请求
    const filterChange = (filterParams) => {
      finished.value = false
      // 合并请求参数，保留之前参数
      reqParams = { ...reqParams, ...filterParams }
      reqParams.page = 1
      goodsList.value = []
    }
    // 需要返回
    return { leimu, getData, goodsList, finished, loading, sortChange, filterChange }
  }
}
</script>
<style scoped lang="less">
.paixu{
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
}
ul {
  width: 1240px;
  display: flex;
  flex-wrap: wrap;
  padding: 0 5px;
  background: #fff;
li {
  margin-right: 20px;
  margin-bottom: 20px;
  &:nth-child(5n) {
    margin-right: 0;
  }
    }
  }
</style>
